package cc.mrbird.plan.service.impl;

import cc.mrbird.assets.dao.AssetManaMapper;
import cc.mrbird.assets.domain.AssetMana;
import cc.mrbird.common.service.impl.BaseService;
import cc.mrbird.plan.dao.InspectionAssetManaMapper;
import cc.mrbird.plan.dao.InspectionMapper;
import cc.mrbird.plan.domain.Inspection;
import cc.mrbird.plan.domain.Inspection;
import cc.mrbird.plan.domain.InspectionAssetMana;
import cc.mrbird.plan.service.InspectionAssetManaService;
import cc.mrbird.plan.service.InspectionService;
import cc.mrbird.plan.service.InspectionService;
import cc.mrbird.system.domain.User;
import cc.mrbird.system.domain.UserRole;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
@Service("inspectionService")
public class InspectionServiceImpl  extends BaseService<Inspection> implements InspectionService {
    private Logger log = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private AssetManaMapper assetManaMapper;
    @Autowired
    private InspectionMapper inspectionMapper;
    @Autowired
    private InspectionAssetManaMapper inspectionAssetManaMapper;
    @Autowired
    private InspectionAssetManaService inspectionAssetManaService;
    @Override
    public List<AssetMana> findAllPoint() {
        List<AssetMana> allPoint = assetManaMapper.findAllPoint();
        return allPoint;
    }
    @Override
    public void add(Inspection inspection,Integer[] assetManas) {
        inspection.setInspection_status(0); //默认是代提交状态
        this.save(inspection);
        this.setInspectionAssetmanas(inspection,assetManas);
    }
    private void setInspectionAssetmanas(Inspection inspection,Integer[] assetManaIds) {
        for (Integer assetManaId : assetManaIds) {
            InspectionAssetMana ia = new InspectionAssetMana();
            ia.setInspection_id(inspection.getInspection_id());
            ia.setAssetMana_id(assetManaId);
            this.inspectionAssetManaMapper.insert(ia);
        }
    }
   /* @Override
    public void update(Inspection inspection) {
        this.updateNotNull(inspection);
    }*/

    @Override
    public Inspection findById(Integer id) {
        try {
            return this.inspectionMapper.findById(id);
        } catch (Exception e) {
            log.error("error", e);
            return new Inspection();
        }
    }
    @Override
    @Transactional
    public void updateInspection(Inspection inspection, Integer[] assetmanas) {
        this.updateNotNull(inspection);
        Example example = new Example(InspectionAssetMana.class);
        example.createCriteria().andCondition("inspection_id=", inspection.getInspection_id());
        this.inspectionAssetManaMapper.deleteByExample(example);
        setInspectionAssetmanas(inspection, assetmanas);
    }

    @Override
    @Transactional
    public void deleteInspections(String inspectionIds) {
        List<String> list = Arrays.asList(inspectionIds.split(","));
        this.batchDelete(list, "inspection_id", Inspection.class);

        this.inspectionAssetManaService.deleteInspectionAssetManasByInspectionId(inspectionIds);
    }


   /* @Override
    @Transactional
    public void delete(String ids) {
        List<String> list = Arrays.asList(ids.split(","));
        this.batchDelete(list, "inspection_id", Inspection.class);
    }*/
}
